package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.wx.domain.GoodsClassDO;
import com.ruoyi.project.wx.service.GoodsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品分类Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/wx/wxGoodsClass")
public class GoodsClassController extends BaseController
{
	@Autowired
	private GoodsClassService goodsClassService;

	/**
	 * 查询商品分类列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxGoodsClass:list')")
	@GetMapping("/list")
	public AjaxResult list(GoodsClassDO goodsClass)
	{

		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(goodsClass);
		stringObjectMap.put("sort","location");
		stringObjectMap.put("order","asc");
		List<GoodsClassDO> list = goodsClassService.list(stringObjectMap);
		return AjaxResult.success(list);
	}

	/**
	 * 导出商品分类列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxGoodsClass:export')")
	@Log(title = "商品分类", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(GoodsClassDO goodsClass)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(goodsClass);
		List<GoodsClassDO> list = goodsClassService.list(stringObjectMap);
		ExcelUtil<GoodsClassDO> util = new ExcelUtil<GoodsClassDO>(GoodsClassDO.class);
		return util.exportExcel(list, "wxGoodsClass");
	}

	/**
	 * 获取商品分类详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxGoodsClass:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Integer id)
	{
		return AjaxResult.success(goodsClassService.get(id));
	}

	/**
	 * 新增商品分类
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxGoodsClass:add')")
	@Log(title = "商品分类", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody GoodsClassDO goodsClass)
	{
		if(goodsClass.getParentId()>0){
			GoodsClassDO goodsClassDO = goodsClassService.get(goodsClass.getParentId());
			if(goodsClassDO.getParentId()!=0){
				return AjaxResult.error("只能添加两级分类");
			}
		}
		if(goodsClass.getParentId()==0){
			goodsClass.setLevel(0);
		}else{
			goodsClass.setLevel(1);
		}
		goodsClass.setDelFlag(0);
		goodsClass.setGmtCreate(new Date());
		return toAjax(goodsClassService.save(goodsClass));
	}

	/**
	 * 修改商品分类
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxGoodsClass:edit')")
	@Log(title = "商品分类", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody GoodsClassDO goodsClass)
	{
		if(goodsClass.getParentId()>0){
			GoodsClassDO goodsClassDO = goodsClassService.get(goodsClass.getParentId());
			if(goodsClassDO.getParentId()!=0){
				return AjaxResult.error("只能添加两级分类");
			}
		}
		goodsClass.setGmtCreate(new Date());
		return toAjax(goodsClassService.update(goodsClass));
	}

	/**
	 * 删除商品分类
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxGoodsClass:remove')")
	@Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Integer[] ids)
	{
		if(ids.length>0){
			for (Integer id : ids) {
				GoodsClassDO goodsClass = goodsClassService.get(id);
				goodsClass.setDelFlag(1);
				goodsClass.setGmtCreate(new Date());
				goodsClassService.update(goodsClass);
			}
		}
		return toAjax(ids.length);
		//return toAjax(goodsClassService.deleteGoodsClassByIds(ids));
	}
}
